<template>
  <div class="prompt-input-container">
    <div class="input-header">
      <h2>AI 3D模型生成器</h2>
      <p>描述您想要生成的3D模型，AI将为您创建</p>
      <div class="generation-mode-selector">
        <h3>生成模式：</h3>
        <div class="mode-options">
          <label class="mode-option">
            <input
              type="radio"
              v-model="generationMode"
              value="now"
              :disabled="isGenerating"
            />
            <span class="mode-label">
              <span class="mode-icon">⚡</span>
              立即生成
            </span>
          </label>
          <label class="mode-option">
            <input
              type="radio"
              v-model="generationMode"
              value="async"
              :disabled="isGenerating"
            />
            <span class="mode-label">
              <span class="mode-icon">📝</span>
              后台生成
            </span>
          </label>
        </div>
        <div v-if="generationMode === 'async'" class="async-notice">
          <span class="notice-icon">ℹ️</span>
          <span>后台生成模式：提交后可在历史记录中查看进度，支持同时生成多个模型</span>
        </div>
      </div>
    </div>
    
    <div class="input-section">
      <div class="input-mode-selector">
        <h3>输入方式：</h3>
        <div class="mode-options">
          <label class="mode-option">
            <input
              type="radio"
              v-model="inputMode"
              value="text"
              :disabled="isGenerating"
            />
            <span class="mode-label">
              <span class="mode-icon">✏️</span>
              文字描述
            </span>
          </label>
          <label class="mode-option">
            <input
              type="radio"
              v-model="inputMode"
              value="image"
              :disabled="isGenerating"
            />
            <span class="mode-label">
              <span class="mode-icon">🖼️</span>
              上传图片
            </span>
          </label>
        </div>
      </div>
      
      <div class="input-wrapper">
        <!-- 文字输入模式 -->
        <div v-if="inputMode === 'text'" class="text-input-container">
          <textarea
            v-model="prompt"
            placeholder="请描述您想要的3D模型，例如：一只可爱的小猫、现代风格的椅子、科幻机器人等..."
            class="prompt-textarea"
            :disabled="isGenerating"
            @keydown.enter.prevent="handleSubmit"
          ></textarea>
          <div class="char-count">{{ prompt.length }}/500</div>
        </div>
        
        <!-- 图片上传模式 -->
        <div v-else class="image-input-container">
          <div class="upload-area" :class="{ 'dragover': isDragOver }" @drop="handleDrop" @dragover="handleDragOver" @dragleave="handleDragLeave" @click="triggerFileInput">
            <input
              ref="fileInput"
              type="file"
              accept="image/*"
              @change="handleFileSelect"
              style="display: none"
            />
            <div v-if="!selectedImage" class="upload-placeholder">
              <div class="upload-icon">📁</div>
              <p>点击或拖拽图片到此处</p>
              <p class="upload-hint">支持 JPG、PNG、GIF 格式</p>
            </div>
            <div v-else class="image-preview">
              <img :src="imagePreviewUrl" alt="预览图片" />
              <button @click.stop="removeImage" class="remove-btn">×</button>
            </div>
          </div>
        </div>
      </div>
      
      <div class="format-options">
        <h3>选择生成格式：</h3>
        <p class="format-recommend">推荐选择 GLB（网页兼容性最好、体积更小、加载更快）</p>
        <div class="format-grid">
          <button
            v-for="format in formatOptions"
            :key="format.value"
            :class="['format-btn', { active: selectedFormat === format.value }]"
            @click="selectFormat(format.value)"
            :disabled="isGenerating"
          >
            <span class="format-emoji">{{ format.emoji }}</span>
            <span class="format-name">{{ format.name }}</span>
          </button>
        </div>
      </div>
      
      
      <div class="pbr-options">
        <h3>材质选项：</h3>
        <div class="pbr-toggle">
          <label class="toggle-container">
            <input
              type="checkbox"
              v-model="enablePBR"
              :disabled="isGenerating"
              class="toggle-input"
            />
            <span class="toggle-slider"></span>
            <span class="toggle-label">
              <span class="toggle-icon">✨</span>
              开启PBR材质生成
            </span>
          </label>
          <p class="pbr-description">启用后生成具有真实物理材质的模型，文件大小会增加</p>
        </div>
      </div>
      
      <div class="action-buttons">
        <button
          class="generate-btn"
          :class="{ disabled: !canGenerate || isGenerating }"
          @click="handleSubmit"
          :disabled="!canGenerate || isGenerating"
        >
          <span v-if="!isGenerating" class="btn-icon">🎨</span>
          <span v-else class="btn-icon spinning">⏳</span>
          {{ isGenerating ? '生成中...' : '生成3D模型' }}
        </button>
        
        <button
          class="clear-btn"
          @click="clearInput"
          :disabled="isGenerating"
        >
          <span class="btn-icon">🗑️</span>
          清空
        </button>
      </div>
    </div>
    
    <div class="example-prompts">
      <h3>示例提示词：</h3>
      <div class="example-grid">
        <button
          v-for="example in examplePrompts"
          :key="example"
          class="example-btn"
          @click="useExample(example)"
          :disabled="isGenerating"
        >
          {{ example }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PromptInput',
  props: {
    isGenerating: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      prompt: '',
      selectedFormat: 'GLB',
      enablePBR: false,
      inputMode: 'text', // 'text' 或 'image'
      generationMode: 'now', // 'now' 或 'async'
      selectedImage: null,
      imagePreviewUrl: '',
      isDragOver: false,
      formatOptions: [
        { value: 'OBJ', name: 'OBJ', emoji: '📦' },
        { value: 'GLB', name: 'GLB', emoji: '🎯' },
        { value: 'STL', name: 'STL', emoji: '🔺' },
        { value: 'USDZ', name: 'USDZ', emoji: '📱' },
        { value: 'FBX', name: 'FBX', emoji: '🎬' },
        { value: 'MP4', name: 'MP4', emoji: '🎥' }
      ],
      examplePrompts: [
        '一只可爱的小猫',
        '现代风格的椅子',
        '科幻机器人',
        '美丽的花朵',
        '复古汽车',
        '魔法城堡',
        '未来城市',
        '可爱的小狗'
      ]
    }
  },
  computed: {
    canGenerate() {
      if (this.inputMode === 'text') {
        return this.prompt.trim().length > 0 && this.prompt.length <= 500
      } else {
        return this.selectedImage !== null
      }
    }
  },
  methods: {
    selectFormat(format) {
      this.selectedFormat = format
    },
    
    handleSubmit() {
      if (!this.canGenerate || this.isGenerating) return
      
      const requestData = {
        inputMode: this.inputMode,
        generationMode: this.generationMode,
        prompt: this.inputMode === 'text' ? this.prompt.trim() : '',
        imageFile: this.inputMode === 'image' ? this.selectedImage : null,
        resultFormat: this.selectedFormat,
        enablePBR: this.enablePBR
      }
      
      this.$emit('generate', requestData)
    },
    
    clearInput() {
      this.prompt = ''
      this.selectedFormat = 'GLB'
      this.enablePBR = false
      this.inputMode = 'text'
      this.selectedImage = null
      this.imagePreviewUrl = ''
      this.isDragOver = false
    },
    
    // 图片上传相关方法
    triggerFileInput() {
      this.$refs.fileInput.click()
    },
    
    handleFileSelect(event) {
      const file = event.target.files[0]
      if (file) {
        this.processImageFile(file)
      }
    },
    
    handleDrop(event) {
      event.preventDefault()
      this.isDragOver = false
      const file = event.dataTransfer.files[0]
      if (file && file.type.startsWith('image/')) {
        this.processImageFile(file)
      }
    },
    
    handleDragOver(event) {
      event.preventDefault()
      this.isDragOver = true
    },
    
    handleDragLeave(event) {
      event.preventDefault()
      this.isDragOver = false
    },
    
    processImageFile(file) {
      if (!file.type.startsWith('image/')) {
        alert('请选择图片文件')
        return
      }
      
      if (file.size > 10 * 1024 * 1024) { // 10MB限制
        alert('图片大小不能超过10MB')
        return
      }
      
      this.selectedImage = file
      
      // 创建预览URL
      const reader = new FileReader()
      reader.onload = (e) => {
        this.imagePreviewUrl = e.target.result
      }
      reader.readAsDataURL(file)
    },
    
    removeImage() {
      this.selectedImage = null
      this.imagePreviewUrl = ''
      this.$refs.fileInput.value = ''
    },
    
    useExample(example) {
      this.prompt = example
    }
  }
}
</script>

<style scoped>
.prompt-input-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.input-header {
  text-align: center;
  margin-bottom: 40px;
}

.input-header h2 {
  font-size: 32px;
  color: #333;
  margin: 0 0 10px 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.input-header p {
  font-size: 16px;
  color: #666;
  margin: 0;
}

.input-section {
  margin-bottom: 40px;
}

.input-wrapper {
  position: relative;
  margin-bottom: 30px;
}

.prompt-textarea {
  width: 100%;
  min-height: 120px;
  padding: 20px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 16px;
  font-family: inherit;
  resize: vertical;
  transition: all 0.3s ease;
  background: #fafbfc;
}

.prompt-textarea:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.prompt-textarea:disabled {
  background: #f5f5f5;
  cursor: not-allowed;
}

.char-count {
  position: absolute;
  bottom: 10px;
  right: 15px;
  font-size: 12px;
  color: #999;
}

.generation-mode-selector {
  margin-bottom: 20px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.generation-mode-selector h3 {
  margin: 0 0 12px 0;
  font-size: 16px;
  color: #333;
}

.async-notice {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 6px 10px;
  background: #e3f2fd;
  border: 1px solid #bbdefb;
  color: #1976d2;
  border-radius: 4px;
  font-size: 12px;
}

.notice-icon {
  font-size: 14px;
}

.input-mode-selector, .format-options, .pbr-options {
  margin-bottom: 30px;
}

.input-mode-selector h3, .format-options h3, .pbr-options h3 {
  font-size: 18px;
  color: #333;
  margin: 0 0 15px 0;
}

.mode-options {
  display: flex;
  gap: 20px;
}

.mode-option {
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.mode-option input[type="radio"] {
  margin-right: 8px;
  transform: scale(1.2);
}

.mode-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 16px;
  color: #333;
}

.mode-icon {
  font-size: 18px;
}

.text-input-container {
  position: relative;
}

.image-input-container {
  position: relative;
}

.upload-area {
  border: 2px dashed #ccc;
  border-radius: 12px;
  padding: 40px 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #fafbfc;
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  box-sizing: border-box;
}

.upload-area:hover {
  border-color: #667eea;
  background: #f0f2ff;
}

.upload-area.dragover {
  border-color: #667eea;
  background: #e6f0ff;
  transform: scale(1.02);
}

.upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.upload-icon {
  font-size: 48px;
  opacity: 0.6;
}

.upload-placeholder p {
  margin: 0;
  color: #666;
  font-size: 16px;
}

.upload-hint {
  font-size: 14px !important;
  color: #999 !important;
}

.image-preview {
  position: relative;
  max-width: 200px;
  max-height: 200px;
}

.image-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

.remove-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #ff4757;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.remove-btn:hover {
  background: #ff3742;
  transform: scale(1.1);
}

.format-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 10px;
}

.format-recommend {
  margin: -8px 0 10px 0;
  color: #667eea;
  font-size: 13px;
}

.format-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 15px 8px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
}

.format-btn:hover:not(:disabled) {
  border-color: #667eea;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}

.format-btn.active {
  border-color: #667eea;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.format-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.format-emoji {
  font-size: 20px;
  margin-bottom: 5px;
}


.pbr-toggle {
  background: #f8f9fa;
  border: 1px solid #e1e5e9;
  border-radius: 12px;
  padding: 20px;
}

.toggle-container {
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.toggle-input {
  display: none;
}

.toggle-slider {
  position: relative;
  width: 50px;
  height: 26px;
  background: #ccc;
  border-radius: 13px;
  transition: all 0.3s ease;
  margin-right: 15px;
  flex-shrink: 0;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 22px;
  height: 22px;
  background: white;
  border-radius: 50%;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toggle-input:checked + .toggle-slider {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.toggle-input:checked + .toggle-slider::before {
  transform: translateX(24px);
}

.toggle-label {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.toggle-icon {
  font-size: 18px;
  margin-right: 8px;
}

.pbr-description {
  margin: 10px 0 0 65px;
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}

.toggle-container:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.action-buttons {
  display: flex;
  gap: 15px;
  justify-content: center;
}

.generate-btn, .clear-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 15px 30px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 150px;
  justify-content: center;
}

.generate-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.generate-btn:hover:not(.disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.generate-btn.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.clear-btn {
  background: #f8f9fa;
  color: #666;
  border: 2px solid #e1e5e9;
}

.clear-btn:hover:not(:disabled) {
  background: #e9ecef;
  transform: translateY(-2px);
}

.clear-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 18px;
}

.btn-icon.spinning {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.example-prompts {
  border-top: 1px solid #e1e5e9;
  padding-top: 30px;
}

.example-prompts h3 {
  font-size: 18px;
  color: #333;
  margin: 0 0 15px 0;
}

.example-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}

.example-btn {
  padding: 10px 15px;
  border: 1px solid #e1e5e9;
  border-radius: 8px;
  background: #f8f9fa;
  color: #666;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  text-align: center;
}

.example-btn:hover:not(:disabled) {
  border-color: #667eea;
  background: #f0f2ff;
  color: #667eea;
}

.example-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .prompt-input-container {
    padding: 20px 15px;
  }
  
  .input-header h2 {
    font-size: 24px;
  }
  
  .format-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .generate-btn, .clear-btn {
    width: 100%;
  }
}
</style>
